package com.uphone.kingmall.adapter;


import android.content.Context;
import android.widget.ImageView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SizeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.china.zhuoyue.R;
import com.uphone.kingmall.bean.GoodsListBean;
import com.uphone.kingmall.utils.GlideUtils;

/**
 * 商品列表两种样式布局
 */

public class SearchGoodsListAdapter extends BaseQuickAdapter<GoodsListBean.DataBean, BaseViewHolder> {

    private GridLayoutManager manager;
    private RecyclerView recyclerview;
    private Context ctx;
    private int item_grid = 0;
    private int item_liner = 1;
    private int type = 1;

    public SearchGoodsListAdapter(Context context, RecyclerView recyclerView, GridLayoutManager manager) {
        super(R.layout.kingmall_item_goodslist_liner);
        this.manager = manager;
        this.recyclerview = recyclerView;
        this.ctx = context;
    }

    public SearchGoodsListAdapter(Context context) {
        super(R.layout.kingmall_item_goodslist_grid);
        this.ctx = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, GoodsListBean.DataBean item) {
        helper.setIsRecyclable(false);
        ImageView icon = helper.getView(R.id.iv_icon);
        GlideUtils.getInstance().loadCornerImage(mContext, item.getGoodsMainImg(), SizeUtils.dp2px(2), icon);
        helper.setText(R.id.tv_name, item.getGoodsName());
        helper.setText(R.id.tv_price, "¥" + item.getGoodsPrice());
        helper.setText(R.id.tv_yue_xiao, "月销" + item.getGoodsSaleNum());
    }

    public int switchType() {
        if (type == item_grid) {
            type = item_liner;
            mLayoutResId = R.layout.kingmall_item_goodslist_liner;
            if (manager != null) {
                manager.setSpanCount(1);
            }
        } else {
            type = item_grid;
            if (manager != null) {
                manager.setSpanCount(2);
            }
            mLayoutResId = R.layout.kingmall_item_goodslist_grid;
        }
        recyclerview.setLayoutManager(manager);
        notifyDataSetChanged();
        return type;
    }


}